package com.apache.passport.service.impl;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.passport.entity.OutwardSys;
import com.apache.passport.manager.OutwardSysManager;

import java.util.List;
import java.util.Map;

/**
 * description:  外部系统记录
 *
 * @author Hou Dayu 创建时间：2016-12-18
 */
public class OutwardSysManagerImpl implements OutwardSysManager {

    protected IDao outwardSysDao;

    protected final String entityName = "com.apache.passport.entity.OutwardSys";

    protected Map<String, PluginConnector> plugins;

    public void setPlugins(Map<String, PluginConnector> plugins) {
        this.plugins = plugins;
    }

    public void setOutwardSysDao(IDao outwardSysDao) {
        this.outwardSysDao = outwardSysDao;
    }

    public String saveInfo(ParamsVo<OutwardSys> vo) throws BusinessException {
        OutwardSys outwardSys = vo.getObj();
        String infoId = Validator.generate();
        //定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
        String cacheKey = "";
        //String cacheKey="outwardSys_"+infoId
        outwardSys.setSysId(infoId);
        MethodParam param = new MethodParam("OutwardSys", cacheKey, "", entityName);
        param.setVaule(outwardSys);
        if (outwardSysDao.insert(param)) {
            return infoId;
        }
        return "";
    }

    public boolean editInfo(ParamsVo<OutwardSys> vo) throws BusinessException {
        OutwardSys outwardSys = vo.getObj();
        if (Validator.isNotNull(outwardSys.getSysId())) {
            String cacheKey = "";
            //String cacheKey="outwardSys_"+outwardSys.getSysId()
            MethodParam param = new MethodParam("OutwardSys", cacheKey, "", entityName);
            param.setVaule(outwardSys);
            return outwardSysDao.edit(param);
        }
        return false;
    }

    public boolean deleteInfo(ParamsVo<OutwardSys> vo) throws BusinessException {
        String infoId = vo.getInfoId();
        if (Validator.isNull(infoId)) {
            return false;
        }
        String cacheKey = "";
        //String cacheKey="outwardSys_"+infoId
        String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        OutwardSys info = (OutwardSys) outwardSysDao.selectById(param);
        if (Validator.isEmpty(info)) {
            return false;
        }
        param.setVaule(info);//此处需要先将状态值赋值为删除状态
        if ("false".equals(mark)) {//逻辑删除
            param.setKey("OutwardSys");
            return outwardSysDao.edit(param);
        } else {
            param.setParams("sysId", infoId);
            param.setDelete(true);
            return outwardSysDao.delete(param);
        }
    }

    public Object getInfoById(ParamsVo<OutwardSys> vo) {
        String infoId = vo.getInfoId();
        String cacheKey = "";
        //String cacheKey="outwardSys_"+infoId
        if (Validator.isNull(infoId)) {
            return null;
        }
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        return outwardSysDao.selectById(param);
    }

    public Object execute(ParamsVo<OutwardSys> vo) {
        String key = vo.getMethodKey();
        try {
            if (!Validator.isEmpty(plugins)) {
                if (!Validator.isEmpty(plugins.get(key))) {
                    return plugins.get(key).execute(vo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    public Page getPageInfo(ParamsVo<OutwardSys> vo) {
        MethodParam param = setMethodParams(vo, 2);
        int pageSize = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
        int pageIndex = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
        param.setPageIndex(pageIndex);
        param.setPageSize(pageSize);
        Page page = outwardSysDao.pageSelect(param);
        return page;
    }

    public List<OutwardSys> getList(ParamsVo<OutwardSys> vo) {
        MethodParam param = setMethodParams(vo, 2);
        return outwardSysDao.select(param);
    }

    public long countInfo(ParamsVo<OutwardSys> vo) {
        MethodParam param = setMethodParams(vo, 1);
        return outwardSysDao.count(param);
    }

    private MethodParam setMethodParams(ParamsVo<OutwardSys> vo, int type) {
        String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
        StringBuilder sb = new StringBuilder(outwardSysDao.getSql(type));
        MethodParam param = new MethodParam(methodKey, "", "", entityName);
        OutwardSys outwardSys = vo.getObj();
        if (Validator.isNotNull(outwardSys.getSysEname())) {
            sb.append(" and sysEname = :sysEname");
            param.setParams("sysEname", outwardSys.getSysEname());
        }
        if (Validator.isNotNull(outwardSys.getSysCname())) {
            sb.append(" and sysCname = :sysCname");
            param.setParams("sysCname", outwardSys.getSysCname());
        }
        if (Validator.isNotNull(outwardSys.getSysRegtime())) {
            sb.append(" and sysRegtime = :sysRegtime");
            param.setParams("sysRegtime", outwardSys.getSysRegtime());
        }
        if (Validator.isNotNull(outwardSys.getSysUpdatetime())) {
            sb.append(" and sysUpdatetime = :sysUpdatetime");
            param.setParams("sysUpdatetime", outwardSys.getSysUpdatetime());
        }
        if (Validator.isNotNull(outwardSys.getSysStatus())) {
            sb.append(" and sysStatus = :sysStatus");
            param.setParams("sysStatus", outwardSys.getSysStatus());
        }
        if (Validator.isNotNull(outwardSys.getSysRemark())) {
            sb.append(" and sysRemark = :sysRemark");
            param.setParams("sysRemark", outwardSys.getSysRemark());
        }
        if (Validator.isNotNull(outwardSys.getSysAccreditip())) {
            sb.append(" and sysAccreditip = :sysAccreditip");
            param.setParams("sysAccreditip", outwardSys.getSysAccreditip());
        }
        if (Validator.isNotNull(outwardSys.getSysAccreditename())) {
            sb.append(" and sysAccreditename = :sysAccreditename");
            param.setParams("sysAccreditename", outwardSys.getSysAccreditename());
        }
        if (Validator.isNotNull(outwardSys.getSysInterface())) {
            sb.append(" and sysInterface = :sysInterface");
            param.setParams("sysInterface", outwardSys.getSysInterface());
        }
        if (Validator.isNotNull(outwardSys.getSysOwtype())) {
            sb.append(" and sysOwtype = :sysOwtype");
            param.setParams("sysOwtype", outwardSys.getSysOwtype());
        }

        param.setSqlStr(sb.toString());
        return param;
    }
}
